package cn.tannn.ops.pwd.strategy;


import cn.tannn.ops.pwd.enums.EncryptType;
import cn.tannn.ops.pwd.strategy.impl.EncryptStrategyAES;
import cn.tannn.ops.pwd.strategy.impl.EncryptStrategyDES;
import cn.tannn.ops.pwd.strategy.impl.EncryptStrategyHS256;
import cn.tannn.ops.pwd.strategy.impl.EncryptStrategyRSA;

/**
 * 加解密 上下文
 * 传入策略并使用
 *
 * @author tnnn
 * @date 2023-02-20 10:31:10
 */
public class EncryptContext {
    EncryptStrategy strategy;

    public EncryptContext(EncryptType type) throws IllegalAccessException {
        if (type == EncryptType.AES) {
            this.strategy = new EncryptStrategyAES();
        } else if (type == EncryptType.DES) {
            this.strategy = new EncryptStrategyDES();
        } else if (type == EncryptType.HS256) {
            this.strategy = new EncryptStrategyHS256();
        } else if (type == EncryptType.RSA) {
            this.strategy = new EncryptStrategyRSA();
        } else {
            throw new IllegalAccessException("非法的算法");
        }
    }

    /**
     * 解密
     *
     * @param secret 密钥
     * @param content 待解密字符
     * @param count 解密次数（跟加密要相同哦）
     * @return 铭文
     */
    public String executeDecrypt(String secret, String content, Integer count) {
        return this.strategy.decrypt(secret, content, count);
    }


    /**
     * 加密
     *
     * @param secret 密钥
     * @param content 待加密字符
     * @param count 加密次数（跟解密要相同哦）
     * @return 密文
     */
    public String executeEncrypt(String secret, String content, Integer count) {
        return this.strategy.encrypt(secret, content, count);
    }
}
